import { BaseScene } from './BaseScene'
import { GameUtils } from '../utils/GameUtils'

export class ResultScene extends BaseScene {
  constructor(game) {
    super(game)
    
    // 获取游戏尺寸
    const size = this.game.getSize()
    this.width = size.width
    this.height = size.height
    
    // 游戏结果数据
    this.result = {
      winner: null,
      score: 0,
      rounds: 0,
      players: []
    }
    
    // UI元素
    this.buttons = {
      playAgain: {
        x: this.width / 2 - 100,
        y: this.height / 2 + 50,
        width: 200,
        height: 50,
        text: '再玩一次'
      },
      backToLobby: {
        x: this.width / 2 - 100,
        y: this.height / 2 + 120,
        width: 200,
        height: 50,
        text: '返回大厅'
      },
      share: {
        x: this.width / 2 - 100,
        y: this.height / 2 + 190,
        width: 200,
        height: 50,
        text: '分享战绩'
      }
    }
    
    // 绑定事件处理函数
    this.boundTouchStartHandler = this.onTouchStart.bind(this)
  }
  
  init(result) {
    // 设置结果数据
    if (result) {
      this.result = { ...result }
    }
    
    // 添加事件监听
    const canvas = this.game.getContext().canvas
    canvas.addEventListener('touchstart', this.boundTouchStartHandler)
  }
  
  render(ctx) {
    // 绘制背景
    ctx.fillStyle = '#f0f0f0'
    ctx.fillRect(0, 0, this.width, this.height)
    
    // 绘制标题
    ctx.fillStyle = '#333'
    ctx.font = 'bold 36px Arial'
    ctx.textAlign = 'center'
    ctx.fillText('游戏结果', this.width / 2, 100)
    
    // 绘制结果信息
    this.drawResult(ctx)
    
    // 绘制按钮
    Object.values(this.buttons).forEach(button => {
      this.drawButton(ctx, button)
    })
  }
  
  drawResult(ctx) {
    ctx.fillStyle = '#333'
    ctx.font = '24px Arial'
    ctx.textAlign = 'center'
    
    // 绘制胜利者
    if (this.result.winner) {
      ctx.fillText(
        `胜利者: ${this.result.winner}`,
        this.width / 2,
        160
      )
    }
    
    // 绘制得分
    ctx.fillText(
      `得分: ${this.result.score}`,
      this.width / 2,
      200
    )
    
    // 绘制回合数
    ctx.fillText(
      `总回合: ${this.result.rounds}`,
      this.width / 2,
      240
    )
    
    // 绘制玩家列表
    if (this.result.players.length > 0) {
      ctx.font = '20px Arial'
      this.result.players.forEach((player, index) => {
        ctx.fillText(
          `${player.name}: ${player.score}分`,
          this.width / 2,
          280 + index * 30
        )
      })
    }
  }
  
  drawButton(ctx, button) {
    // 绘制按钮背景
    ctx.fillStyle = '#4CAF50'
    ctx.beginPath()
    ctx.roundRect(button.x, button.y, button.width, button.height, 5)
    ctx.fill()
    
    // 绘制按钮文字
    ctx.fillStyle = '#FFFFFF'
    ctx.font = '20px Arial'
    ctx.textAlign = 'center'
    ctx.textBaseline = 'middle'
    ctx.fillText(button.text, button.x + button.width / 2, button.y + button.height / 2)
  }
  
  onTouchStart(e) {
    const canvas = this.game.getContext().canvas
    const rect = canvas.getBoundingClientRect()
    const x = e.touches[0].clientX - rect.left
    const y = e.touches[0].clientY - rect.top
    
    // 检查按钮点击
    Object.entries(this.buttons).forEach(([key, button]) => {
      if (this.isPointInRect(x, y, button)) {
        this.handleButtonClick(key)
      }
    })
  }
  
  isPointInRect(x, y, rect) {
    return x >= rect.x && x <= rect.x + rect.width &&
           y >= rect.y && y <= rect.y + rect.height
  }
  
  handleButtonClick(buttonKey) {
    // 播放点击音效
    this.game.getResourceManager().playSound('click')
    
    switch (buttonKey) {
      case 'playAgain':
        this.playAgain()
        break
      case 'backToLobby':
        this.backToLobby()
        break
      case 'share':
        this.shareResult()
        break
    }
  }
  
  playAgain() {
    // 重新开始游戏
    this.game.getStateManager().switchScene('game')
  }
  
  backToLobby() {
    // 返回大厅
    this.game.getStateManager().switchScene('lobby')
  }
  
  shareResult() {
    // 分享结果
    wx.shareAppMessage({
      title: '真心话大冒险游戏结果',
      imageUrl: '', // TODO: 添加分享图片
      query: `score=${this.result.score}&rounds=${this.result.rounds}`,
      success: () => {
        wx.showToast({
          title: '分享成功',
          icon: 'success'
        })
      },
      fail: () => {
        wx.showToast({
          title: '分享失败',
          icon: 'none'
        })
      }
    })
  }
  
  destroy() {
    // 移除事件监听
    const canvas = this.game.getContext().canvas
    canvas.removeEventListener('touchstart', this.boundTouchStartHandler)
  }
} 